package com.partner.action.login;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import com.partner.bean.login.User;
import com.partner.bean.login.UserDetail;
import com.partner.bean.login.UserForgetPwd;
import com.partner.bean.login.UserGetCaptcha;
import com.partner.bean.login.UserCheckCaptcha;
import com.partner.bean.login.Userinfo;
import com.partner.bean.login.UserinfoGet;
import com.partner.bean.login.UserinfoResetPwd;
import com.partner.bean.login.UserinfoUpdate;
import com.partner.bean.sms.Sms;
import com.partner.bean.suggestion.Suggestion;
import com.partner.bean.sysright.TSysrightVo;
import com.partner.bean.sysright.TSysuser;
import com.partner.bean.sysright.TSysuserVo;
import com.partner.frame.BaseAction;
import com.partner.frame.Result;
import com.partner.frame.Sendsms;
import com.partner.frame.common.CommonInstans;
import com.partner.frame.common.ConstantsMark;
import com.partner.frame.support.util.CommonUtil;
import com.partner.frame.support.util.CreateYzm;
import com.partner.frame.support.util.TujiPassWordEncoder;
import com.partner.service.login.UserinfoService;
import com.partner.service.radis.RedisCacheUtil;
import com.partner.service.sms.SmsService;
import com.partner.service.suggestion.SuggestionService;
import com.partner.service.sysright.SysManagerService;
import com.partner.service.sysright.SysRightManagerService;

@RestController
public class UserinfoAction extends BaseAction {
	java.text.DateFormat dateformat = new java.text.SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	// 自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错
	@Autowired
	private UserinfoService userinfoService;
	@Autowired
	private SmsService smsService;
	@Autowired
	private SuggestionService suggestionService;
	@Autowired
	private SysManagerService sysManagerService;
	@Autowired
	private SysRightManagerService sysRightManagerService;

	// 用户登录
		@RequestMapping(value="/loginSuccess")
		public ModelAndView login(ModelAndView mv, HttpServletRequest request) throws Exception {

			UserDetail userDetails = (UserDetail) SecurityContextHolder.getContext()
				    .getAuthentication()
				    .getPrincipal();
			String username = userDetails.getUsername(); //登录名
			HttpSession session = request.getSession();
			TSysuser tSysuser = new TSysuser();
			tSysuser.setUsername(username);
			//根据登录名查询出登录用户
			TSysuserVo sysUserbyUsername = sysManagerService.getSysUserbyUsername(tSysuser);
			//当前角色拥有的所有权限
			List<TSysrightVo> rights = sysRightManagerService.selectRightByRole(sysUserbyUsername.getRoleid(),session);
			
			
			//把用户名保存到session
			session.setAttribute(CommonInstans.SYSUSERNAME,username);
			session.setAttribute(CommonInstans.LOGINUSERID,sysUserbyUsername.getId());
			System.out.println("request.getContextPath()="+request.getContextPath());
			session.setAttribute("ctx", request.getContextPath());
			
			mv.addObject("user", sysUserbyUsername); 
			mv.addObject("rights", rights);
	        mv.setViewName("/index");
	        return mv ; 
			
		}
	
	

	// 注册
	@PostMapping("/userinfo/register")
	public Result registe(@Valid Userinfo objBean, BindingResult result,
			HttpServletRequest request, HttpSession session) {

		Result rs = new Result();
		rs.setStatus(ConstantsMark.COMMON_ERROR);
		rs.setObject(ConstantsMark.COMMON_BLANK);

		// md5加密
		String md5pwd = TujiPassWordEncoder.MD5(objBean.getPassword());
		objBean.setPassword(md5pwd);
		String sessionid = session.getId() + new Date().getTime();
		String md5session = TujiPassWordEncoder.MD5(sessionid);
		objBean.setSession(md5session);
		rs.setObject(objBean);
		objBean.setLastlogintime(dateformat.format(new Date()));
		objBean.setOnline(CommonInstans.YES_TAG);// 登录成功，记录在线
		objBean.setSession(md5session);
		rs.setDesc(ConstantsMark.REGIST_SUCCESS);
		rs.setStatus(ConstantsMark.SUCCESS);

		return rs;
	}

	// 注册第一步判断验证码是否正确
	@PostMapping("/userinfo/registercheckcode")
	public Result registercheckcode(@Valid UserCheckCaptcha objBean,
			BindingResult result, HttpServletRequest request,
			HttpSession session) {
		Result rs = new Result();
		rs.setStatus(ConstantsMark.COMMON_ERROR);
		rs.setObject(ConstantsMark.COMMON_BLANK);
		rs.setStatus(ConstantsMark.SUCCESS);
		return rs;
	}

	/**
	 * 忘记密码
	 * 
	 * @param objBean
	 * @param captcha
	 * @param account
	 * @param new password
	 * @return
	 */
	@PostMapping("/userinfo/updatePwd")
	public Result updatePwd(@Valid UserForgetPwd objBean, BindingResult result,
			HttpServletRequest request, HttpSession session) {

		Result rs = new Result();
		rs.setStatus(ConstantsMark.COMMON_ERROR);
		rs.setObject(ConstantsMark.COMMON_BLANK);

		String captcha = objBean.getCaptcha();
		rs.setDesc(ConstantsMark.PWDRESET_SUCCESS);
		rs.setStatus(ConstantsMark.SUCCESS);

		return rs;
	}

	// 获得手机验证码
	@PostMapping("/userinfo/getCaptcha")
	public Result getCaptcha(@Valid UserGetCaptcha objBean,
			BindingResult result, HttpServletRequest request,
			HttpSession session) {
		Result rs = new Result();
		rs.setStatus(ConstantsMark.COMMON_ERROR);
		rs.setObject(ConstantsMark.COMMON_BLANK);
		String account = objBean.getAccount();

		return rs;
	}



	/**
	 * 重置密码
	 * 
	 * @param objBean
	 * @param result
	 * @param session
	 *            ,account,旧密码，新密码
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/userinfo/resetPwd")
	public Result resetPwd(@Valid UserinfoResetPwd o, BindingResult result)
			throws Exception {
		Result rs = new Result();
		rs.setStatus(ConstantsMark.COMMON_ERROR);
		rs.setObject(ConstantsMark.COMMON_BLANK);

		return rs;

	}

	// 获取个人信息
	@PostMapping("/userinfo/getUserinfo")
	public Result getUserinfo(@Valid UserinfoGet o, BindingResult result)
			throws Exception {
		Result rs = new Result();
		rs.setStatus(ConstantsMark.COMMON_ERROR);
		rs.setDesc(ConstantsMark.COMMON_BLANK);

		return rs;
	}

	// 修改个人信息
	@PostMapping("/userinfo/updateUserinfo")
	public Result updateUserinfo(@Valid UserinfoUpdate o, BindingResult result)
			throws Exception {
		Result rs = new Result();
		rs.setStatus(ConstantsMark.COMMON_ERROR);
		rs.setObject(ConstantsMark.COMMON_BLANK);

		return rs;
	}

	// 反馈建议
	@PostMapping("/userinfo/submitSuggestion")
	public Result submitSuggestion(@Valid Suggestion o, BindingResult result)
			throws Exception {
		Result rs = new Result();
		rs.setStatus(ConstantsMark.COMMON_ERROR);
		rs.setObject(ConstantsMark.COMMON_BLANK);

		return rs;

	}

	// 退出系统
	@PostMapping("/userinfo/quitWc")
	public Result quitWc(@Valid UserinfoGet o, BindingResult result)
			throws Exception {
		Result rs = new Result();
		rs.setStatus(ConstantsMark.COMMON_ERROR);
		rs.setDesc(ConstantsMark.COMMON_BLANK);

		return rs;

	}
	

}
